package org.example.struct.array;

/**
 * @author liyishan
 * @date 2024/6/12 16:59
 * @apiNote
 */

public class Test00733FloodFill {
    static int[] dx = {-1,1,0,0};
    static int[] dy = {0,0,-1,1};
    public int[][] floodFill(int[][] image, int sr, int sc, int color) {
        int currColor = image[sr][sc];
        if (currColor == color){
            dfs(image,sr,sc,currColor,color);
        }
        return image;
    }
    public static void dfs(int[][] image,int x,int y,int currColor,int color){
        if(image[x][y] == currColor){
            image[x][y] = color;
            for(int i = 0;i < 4;i++){
                int mx = x + dx[i] , my = y + dy[i];
                if(mx >=0 && mx <image.length && my >=0 && my < image[0].length){
                    dfs(image,mx,my,currColor,color);
                }
            }
        }
    }
}
